import cv2
import numpy as np
import shutil
import os
import datetime
import time
from overlap import find_longest_prefix_match, count_speed, get_x_t
from slide import Direction

list1 = [1080, 1080, 44, 91, 338, 489, 1080, 607, 885, 1054, 1067, 1080, 1074, 1078, 1079]
list2 = [1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080, 1075, 18, 121, 322, 1080, 147, 276, 898, 991, 1080, 1054, 1056, 1071, 1073, 1080, 1078, 1079, 1080, 1080]

# get_x_t(list2, Direction.Left)
count_speed(list2, 1080)
exit(0)

v_path = 'slide2_1.mp4'

cap = cv2.VideoCapture(v_path)  # 替换 'video.mp4' 为您要处理的视频文件路径
# 读取视频的基本信息
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
frame_rate = int(cap.get(5))

ret, frame1 = cap.read()
ret, frame2 = cap.read()
output_path = f"data/output_{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}"
if os.path.exists(output_path):
    shutil.rmtree(output_path)
os.makedirs(output_path)
os.makedirs(f"{output_path}/orig")
os.makedirs(f"{output_path}/diff")
frame_orig = frame1.copy()
if not ret:
    print("无法读取视频帧")
    cap.release()
    cv2.destroyAllWindows()
    exit()
count = -1

# cv2.imwrite(f"{output_path}/orig/{count}_orig.png", frame1)
t_start = time.time()

lenth_list = list()
while True:
    count += 1
    # 计算当前帧和前一帧的差异
    cv2.imwrite(f"{output_path}/orig/{count}_orig.png", frame1)
    match, orig, max_match_length = find_longest_prefix_match(frame2, frame1, True)
    if match is not None:
        cv2.imwrite(f"{output_path}/diff/{count}_diff.png", orig)
    else:
        print("没有前缀匹配")
    lenth_list.append(max_match_length)
    frame1 = frame2
    ret, frame2 = cap.read()
    if not ret:
        break
    # 退出按键：按'q'键退出循环
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

print(lenth_list)
# 释放视频捕捉对象和关闭窗口
cap.release()
cv2.destroyAllWindows()
t_end = time.time()
print(t_end - t_start)
